﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using ControleRepresentanteSync.Application.DynamicSync;

namespace ControleRepresentanteSync.View.DynamicSync
{
    public abstract class DynamicSyncListViewModel<TContentViewModel> : ViewModelBase, IDynamicSyncListViewModel<TContentViewModel>
        where TContentViewModel : IDynamicSyncContentViewModel
    {
        public static readonly DependencyProperty IsShowIsCheckColumnProperty =
            DependencyProperty.Register("IsShowIsCheckedColumn", typeof(bool), typeof(DynamicSyncListViewModel<TContentViewModel>), new PropertyMetadata(OnIsShowIsCheckedColumnChanged));
        public static readonly DependencyProperty IsAllCheckedProperty =
            DependencyProperty.Register("IsAllChecked", typeof(bool), typeof(DynamicSyncListViewModel<TContentViewModel>), new PropertyMetadata(OnIsAllCheckedChanged));

        private readonly ObservableCollection<TContentViewModel> m_Contents;

        protected DynamicSyncListViewModel()
        {
            m_Contents = new ObservableCollection<TContentViewModel>();
        }

        public IList<TContentViewModel> Contents { get { return m_Contents; } }

        private static void OnIsShowIsCheckedColumnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((DynamicSyncListViewModel<TContentViewModel>)d).FireIsShowIsCheckedColumnChanged();
        }

        private void FireIsShowIsCheckedColumnChanged()
        {
            if (IsShowIsCheckedColumnChanged != null)
                IsShowIsCheckedColumnChanged(this, EventArgs.Empty);
        }

        public event EventHandler IsShowIsCheckedColumnChanged;

        public bool IsShowIsCheckedColumn
        {
            get { return (bool)GetValue(IsShowIsCheckColumnProperty); }
            set { SetValue(IsShowIsCheckColumnProperty, value); }
        }

        private static void OnIsAllCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((DynamicSyncListViewModel<TContentViewModel>)d).FireIsAllCheckedChanged();
        }

        private void FireIsAllCheckedChanged()
        {
            if (IsAllCheckedChanged != null)
                IsAllCheckedChanged(this, EventArgs.Empty);
        }

        public event EventHandler IsAllCheckedChanged;

        public bool IsAllChecked
        {
            get { return (bool)GetValue(IsAllCheckedProperty); }
            set { SetValue(IsAllCheckedProperty, value); }
        }
    }
}
